﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FinancialManagement.Utils;

namespace FinancialManagement.Models.Repository
{
    public class BudgetRepository
    {
        /// <summary>
        /// 根据单位编号获取部门项目预算信息
        /// </summary>
        /// <param name="depId"></param>
        /// <returns></returns>
        internal IList<DepBudgetCollection> GetDepBudgetCollectionsByDepId(string depId)
        {
            using (var dc = new FinancialMGTDataContext())
            {
                var query = from t in dc.T_ProjectVesting
                            select t;
                if (depId == Util.DEPFINANCIAL)
                {
                    return (from t in query
                            select new DepBudgetCollection
                                 {
                                     Notes = t.Notes,
                                     ProjectID = t.ProjectID,
                                     ProjectName = t.T_ProjectTable.ProjectName,
                                     TotalBudget = 0
                                 }).ToList();
                }
                else
                {
                    return (from t in query
                            where t.DepartmentID != null && t.DepartmentID.Equals(depId)
                            select new DepBudgetCollection
                            {
                                Notes = t.Notes,
                                ProjectID = t.ProjectID,
                                ProjectName = t.T_ProjectTable.ProjectName,
                                TotalBudget = 0
                            }).ToList();
                }
            }

        }

        /// <summary>
        /// 根据单位编号和项目编号获取项目预算信息
        /// </summary>
        /// <param name="depId"></param>
        /// <param name="projectId"></param>
        /// <returns></returns>
        internal DepBudgetCollection GetDepBudgetCollectionById(string depId, string projectId)
        {
            using (var dc = new FinancialMGTDataContext())
            {
                if (depId == Util.DEPFINANCIAL)
                {
                    var query = from t in dc.T_ProjectVesting
                                where t.ProjectID != null && t.ProjectID.Equals(projectId)
                                select new DepBudgetCollection
                                {
                                    Notes = t.Notes,
                                    ProjectID = t.ProjectID,
                                    ProjectName = t.T_ProjectTable.ProjectName,
                                    TotalBudget = 0
                                };
                    if (query != null && query.Count() > 0)
                        return query.FirstOrDefault();
                    else
                        return new DepBudgetCollection();
                }
                else
                {
                    var query = from t in dc.T_ProjectVesting
                                where t.DepartmentID != null && t.DepartmentID.Equals(depId) && t.ProjectID != null && t.ProjectID.Equals(projectId)
                                select new DepBudgetCollection
                                {
                                    Notes = t.Notes,
                                    ProjectID = t.ProjectID,
                                    ProjectName = t.T_ProjectTable.ProjectName,
                                    TotalBudget = 0
                                };
                    if (query != null && query.Count() > 0)
                        return query.FirstOrDefault();
                    else
                        return new DepBudgetCollection();
                }
            }
        }

        #region 目前用不到
        internal void UpdateDepBudgetCollection(DepBudgetCollection product)
        {
            using (var dc = new FinancialMGTDataContext())
            {



            }
        }

        internal void InsertDepBudgetCollection(DepBudgetCollection product)
        {
            //throw new NotImplementedException();
        }

        internal void InsertDepBudgetCollection(DepBudgetCollection product, string depId)
        {
            //throw new NotImplementedException();
        }

        internal void DeleteDepBudgetCollection(DepBudgetCollection product)
        {
            //throw new NotImplementedException();
        }
        #endregion

        /// <summary>
        /// 根据项目编号获取所有对应的明细
        /// </summary>
        /// <param name="projectID"></param>
        /// <returns></returns>
        internal IList<ProjectTbaleDetails> GetDepBudgetProjectDetailByProjectId(string projectID)
        {
            using (var dc = new FinancialMGTDataContext())
            {
                var query = (from t in dc.T_ProjectTbaleDetails
                             where t.ProjectID != null && t.ProjectID.Equals(projectID)
                             select new ProjectTbaleDetails
                             {
                                 DSID = t.DSID,
                                 Annual = t.Annual,
                                 BudgetAccding = t.BudgetAccding,
                                 Illustration = t.Illustration,
                                 Notes = t.Notes,
                                 ProjectBudgetMoney = t.ProjectBudgetMoney,
                                 ProjectBudgetName = t.ProjectBudgetName,
                                 ProjectID = t.ProjectID
                             }).ToList();
                return query;
            }
        }

        /// <summary>
        /// 根据流水获取对应的明细
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        internal ProjectTbaleDetails GetDepBudgetProjectDetailByDSID(int id)
        {
            using (var dc = new FinancialMGTDataContext())
            {
                var query = from t in dc.T_ProjectTbaleDetails
                            where t.DSID == id
                            select new ProjectTbaleDetails
                            {
                                DSID = t.DSID,
                                Annual = t.Annual,
                                BudgetAccding = t.BudgetAccding,
                                Illustration = t.Illustration,
                                Notes = t.Notes,
                                ProjectBudgetMoney = t.ProjectBudgetMoney,
                                ProjectBudgetName = t.ProjectBudgetName,
                                ProjectID = t.ProjectID
                            };
                if (query != null && query.Count() > 0)
                {
                    return query.FirstOrDefault();
                }
                else
                {
                    return new ProjectTbaleDetails();
                }
            }
        }

        /// <summary>
        /// 更新项目预算明细表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        internal bool UpdateDepBudgetProjectDetail(ProjectTbaleDetails model)
        {
            bool isOk = true;
            try
            {
                using (var dc = new FinancialMGTDataContext())
                {
                    var tModel = dc.T_ProjectTbaleDetails.FirstOrDefault(o => o.DSID == model.DSID);
                    if (tModel != null)
                    {
                        tModel.BudgetAccding = model.BudgetAccding;
                        tModel.Annual = Util.GetServerDate().Year;
                        tModel.Illustration = model.Illustration;
                        tModel.Notes = model.Notes;
                        tModel.ProjectBudgetMoney = model.ProjectBudgetMoney;
                        tModel.ProjectBudgetName = model.ProjectBudgetName;
                        dc.SubmitChanges();
                    }
                    else
                    {
                        isOk = false;
                    }
                }
            }
            catch (Exception)
            {
                isOk = false;
            }

            return isOk;
        }

        /// <summary>
        /// 删除项目预算明细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        internal bool DeleteDepBudgetProjectDetailByDSID(int id)
        {
            bool isOk = true;
            try
            {
                using (var dc = new FinancialMGTDataContext())
                {
                    var tModel = dc.T_ProjectTbaleDetails.FirstOrDefault(o => o.DSID == id);
                    if (tModel != null)
                    {
                        dc.T_ProjectTbaleDetails.DeleteOnSubmit(tModel);
                        dc.SubmitChanges();
                    }
                    else
                    {
                        isOk = false;
                    }
                }
            }
            catch (Exception)
            {
                isOk = false;
            }

            return isOk;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        internal bool InsertDepBudgetProjectDetail(ProjectTbaleDetails model)
        {
            bool isOk = true;
            try
            {
                using (var dc = new FinancialMGTDataContext())
                {
                    var tModel = new T_ProjectTbaleDetails();
                    if (tModel != null)
                    {
                        tModel.DSID = Util.GetSerialID(Util.PROJECTTABLEDETAIL);
                        tModel.BudgetAccding = model.BudgetAccding;
                        tModel.Annual = Util.GetServerDate().Year;
                        tModel.Illustration = model.Illustration;
                        tModel.Notes = model.Notes;
                        tModel.ProjectBudgetMoney = model.ProjectBudgetMoney;
                        tModel.ProjectBudgetName = model.ProjectBudgetName;
                        tModel.ProjectID = model.ProjectID;
                        dc.T_ProjectTbaleDetails.InsertOnSubmit(tModel);
                        dc.SubmitChanges();
                    }
                    else
                    {
                        isOk = false;
                    }
                }
            }
            catch (Exception)
            {
                isOk = false;
            }

            return isOk;
        }
    }
}